package com.vchello.controller.user;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.qq.connect.utils.json.JSONArray;
import com.vchello.bean.security.Role;
import com.vchello.bean.security.RoleSecurity;
import com.vchello.bean.security.Security;
import com.vchello.bean.security.UserRole;
import com.vchello.bean.user.User;
import com.vchello.controller.BaseController;
import com.vchello.service.security.RoleSecurityService;
import com.vchello.service.security.RoleService;
import com.vchello.service.security.SecurityService;
import com.vchello.service.security.UserRoleService;
import com.vchello.util.ToolUtil;

/**
 * 
 * @author power
 *
 */
@Controller
@RequestMapping(value = "/role")
public class RoleController extends BaseController {

	private static final String REDIRECT = "redirect:/login";

	@Autowired
	private RoleService roleService;
	@Autowired
	private RoleSecurityService roleSecurityService;
	@Autowired
	private SecurityService securityService;
	@Autowired
	private UserRoleService userRoleService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		User user = getUser();
		if (null == user) {
			return REDIRECT;
		}
		List<Role> roles = roleService.findRoleByUserId(user.getId());
		model.addAttribute("roles", roles);
		return null;

	}

	@RequestMapping(value = "/getRole", method = RequestMethod.POST)
	public void getRole() {
		User user = getUser();
		if (null == user) {
			error("用户未登录");
			return;
		}
		List<Role> roles = roleService.findRoleByUserId(user.getId());
		JSONArray jsonArray = new JSONArray();
		JSONObject json = null;
		for (Role role : roles) {
			json = new JSONObject();
			json.put("roleName", role.getName());
			jsonArray.put(json);
		}
		success(jsonArray.toString());

	}
	
	@RequestMapping(value = "/addRole", method = RequestMethod.GET)
	public String addRole(Model model){
		User user = getUser();
		if (null == user) {
			return REDIRECT;
		}
		List<Security> securitys = securityService.findSecurityByList();
		List<Role> roleList = roleService.findRoleByUserId(user.getId());
		model.addAttribute("securitys", securitys).addAttribute("roleList", roleList).addAttribute("role", new Role());
		return "user/addRole";
		
	}

	@RequestMapping(value = "/addRole", method = RequestMethod.POST)
	public void addRole(Role role, String securityArry) {
		User user = getUser();
		if (null == user) {
			error("用户未登录");
			return;
		}
		role.setUserId(user.getId());
		boolean isTrue = roleService.saveRole(role);
		if (isTrue) {
			RoleSecurity roleSecurity = null;
			List<RoleSecurity> roleSecuritys = new ArrayList<RoleSecurity>();
			for (String securityId : securityArry.split(",")) {
				roleSecurity = new RoleSecurity();
				roleSecurity.setSecurityId(Integer.parseInt(securityId));
				roleSecurity.setRoleId(role.getId());
				roleSecuritys.add(roleSecurity);
			}
			roleSecurityService.saveAllRoleSecurity(roleSecuritys);
			success("新增角色成功");
			return;
		}
		error("新增角色失败");

	}
	
	@RequestMapping(value = "/updateRole/{id}", method = RequestMethod.GET)
	public String updateRole(Model model, @PathVariable int id){
		User user = getUser();
		if (null == user) {
			return REDIRECT;
		}
		Role role = roleService.findByOne(id);
		List<Security> securitys = securityService.findSecurityByList();
		List<Role> roleList = roleService.findRoleByUserId(user.getId());
		model.addAttribute("role", role).addAttribute("securitys", securitys).addAttribute("roleList", roleList);
		return "user/addRole";
	}
	
	@RequestMapping(value = "/updateRole/{id}", method = RequestMethod.POST)
	public void updateRole(@PathVariable int id, String name, String securityArry){
		User user = getUser();
		if (null == user) {
			error("用户未登录");
			return;
		}
		Role role = roleService.findByOne(id);
		if(null == role) {
			error("找不到角色");
			return;
		}
		
		role.setName(name);
		boolean isTrue = roleService.updateRole(role);
		if(isTrue) {
			if(ToolUtil.isNotEmpty(securityArry)) {
				roleSecurityService.deleteByRoleId(role.getId());
				RoleSecurity roleSecurity = null;
				List<RoleSecurity> roleSecuritys = new ArrayList<RoleSecurity>();
				for (String securityId : securityArry.split(",")) {
					roleSecurity = new RoleSecurity();
					roleSecurity.setSecurityId(Integer.parseInt(securityId));
					roleSecurity.setRoleId(role.getId());
					roleSecuritys.add(roleSecurity);
				}
				roleSecurityService.saveAllRoleSecurity(roleSecuritys);
			}
			success("修改角色成功");
			return;
		}
		error("修改角色失败");
	}
	
	@RequestMapping(value = "/deleteRole", method = RequestMethod.POST)
	public void deleteRole(int id){
		User user = getUser();
		if (null == user) {
			error("用户未登录");
			return;
		}
		Role role = roleService.findByOne(id);
		if(null == role) {
			error("找不到角色");
			return;
		}
		List<UserRole> userRoleList = userRoleService.findUserRoleByRoleId(user.getId(), role.getId());
		if(ToolUtil.isArrayListNotEmpty(userRoleList)) {
			error("该角色还有用户，请解除用户关系再删除");
			return;
		}
		boolean isTrue = roleService.deleteRole(role);
		if(isTrue) {
			success("删除角色成功");
			return;
		}
		error("删除角色失败");
	}
}
